﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

//////////////////////////////////////////////////////////////////////////
//Advisor_AddCourses   .aspx.cs                                         //
//Last Edited: 21 Mar 2012                                              //
//                                                                      //
//This file contains functionality to add courses to the database       //
//////////////////////////////////////////////////////////////////////////
public partial class Advisor_AddCourses : System.Web.UI.Page
{
    //////////////////
    //Page Functions//
    //////////////////

    /// <summary>
    /// Page_Load()
    /// 
    /// Inintializes the page
    /// </summary>
    /// <param name="sender">object sender (unused, required by ASP.NET) - web control that called the function</param>
    /// <param name="e">EventArgs e (unused, required by ASP.NET) - other arguments for the function</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        //If this is the first time the page has been loaded
        if (!IsPostBack)
        {
            //Populate Subject Dropdown
            Dictionary<String, String> courses = SharedFunctions.getDptAcronymToString();
            SubjectList.Items.Clear();
            SubjectList.Items.Add(new ListItem("Choose Subject"));
            foreach (String s in courses.Keys)
            {
                SubjectList.Items.Add(new ListItem(courses[s], s));
            }
            //Get all the courses in the databases to add to the list of corequisites and prerequisites
            List<Dictionary<String, Object>> results = StoredProcedures.getListOfCourses();
            foreach (Dictionary<String, Object> i in results)
            {
                String courseInfo = "" + i["dpt"] + " " + i["num"] + ": " + i["name"] + ", Credits: " + i["credits"];
                CoreqChoiceList.Items.Add(new ListItem(courseInfo, "" + i["courseID"]));
                PrereqChoiceList.Items.Add(new ListItem(courseInfo, "" + i["courseID"]));
            }
            //Add the three possible tags
            TagChoiceList.Items.Add(new ListItem("Fall", "1"));
            TagChoiceList.Items.Add(new ListItem("Spring", "2"));
            TagChoiceList.Items.Add(new ListItem("Summer", "3"));
        }
    } //end Page_Load()

    /// <summary>
    /// HomeButton_Click()
    /// 
    /// Redirects the user to the default page for advisor's
    /// </summary>
    /// <param name="sender">object sender (unused, required by ASP.NET) - web control that called the function</param>
    /// <param name="e">EventArgs e (unused, required by ASP.NET) - other arguments for the function</param>
    protected void HomeButton_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Advisor/Default.aspx");
    } //end HomeButton_Click()

    /// <summary>
    /// HelpButton_Click()
    /// 
    /// Redirects the user to the help page for advisor's
    /// </summary>
    /// <param name="sender">object sender (unused, required by ASP.NET) - web control that called the function</param>
    /// <param name="e">EventArgs e (unused, required by ASP.NET) - other arguments for the function</param>
    protected void HelpButton_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Advisor/Help.aspx");
    } //end HelpButton_Click()

    /// <summary>
    /// AddPreReq_Click()
    /// 
    /// Adds a prerequisite to the list of prerequisites for a course
    /// </summary>
    /// <param name="sender">object sender (unused, required by ASP.NET) - web control that called the function</param>
    /// <param name="e">EventArgs e (unused, required by ASP.NET) - other arguments for the function</param>
    protected void AddPreReq_Click(object sender, EventArgs e)
    {
        //If there is no prerequisite choice, return out
        if (PrereqChoiceList.SelectedItem == null)
            return;
        //Add the chosen prerequisite to the list of prerequisites and remove it from the choices of prequisites
        PrereqList.Items.Add(PrereqChoiceList.SelectedItem);
        PrereqList.ClearSelection();
        PrereqChoiceList.Items.Remove(PrereqChoiceList.SelectedItem);
        PrereqChoiceList.ClearSelection();
    } //end AddPreReq_Click()

    /// <summary>
    /// AddCoReq_Click()
    /// 
    /// Adds a corequisite to the list of corequisites for a course
    /// </summary>
    /// <param name="sender">object sender (unused, required by ASP.NET) - web control that called the function</param>
    /// <param name="e">EventArgs e (unused, required by ASP.NET) - other arguments for the function</param>
    protected void AddCoReq_Click(object sender, EventArgs e)
    {
        //If there is no corequisite choice, return out
        if (CoreqChoiceList.SelectedItem == null)
            return;
        //Add the chosen corequisite to the list of corequisites and remove it from the choice of corequisites
        CoreqList.Items.Add(CoreqChoiceList.SelectedItem);
        CoreqList.ClearSelection();
        CoreqChoiceList.Items.Remove(CoreqChoiceList.SelectedItem);
        CoreqChoiceList.ClearSelection();
    } //end AddCoReq_Click()

    /// <summary>
    /// AddTag_Click()
    /// 
    /// Adds a tag to the list of tags for a course
    /// </summary>
    /// <param name="sender">object sender (unused, required by ASP.NET) - web control that called the function</param>
    /// <param name="e">EventArgs e (unused, required by ASP.NET) - other arguments for the function</param>
    protected void AddTag_Click(object sender, EventArgs e)
    {
        //If there is no tag choice, return out
        if (TagChoiceList.SelectedItem == null)
            return;
        //Add the chosen tag to the list of tags and remove it from the choice of tags
        TagList.Items.Add(TagChoiceList.SelectedItem);
        TagList.ClearSelection();
        TagChoiceList.Items.Remove(TagChoiceList.SelectedItem);
        TagChoiceList.ClearSelection();
    } //end AddTag_Click()

    /// <summary>
    /// RemovePreReq_Click()
    /// 
    /// Removes a prerequisite from the list of prerequisites for a course
    /// </summary>
    /// <param name="sender">object sender (unused, required by ASP.NET) - web control that called the function</param>
    /// <param name="e">EventArgs e (unused, required by ASP.NET) - other arguments for the function</param>
    protected void RemovePreReq_Click(object sender, EventArgs e)
    {
        //If there is no prerequisite choice, return out
        if (PrereqList.SelectedItem == null)
            return;
        //Removes the chosen prerequisite from the existing prerequisites for the course and add it to the list of possible prequisites
        PrereqChoiceList.Items.Add(PrereqList.SelectedItem);
        PrereqList.Items.Remove(PrereqList.SelectedItem);
        PrereqChoiceList.ClearSelection();
        PrereqList.ClearSelection();
    } //end RemovePreReq_Click()

    /// <summary>
    /// RemoveCoReq_Click()
    /// 
    /// Removes a corequisite from the list of corequisites for a course
    /// </summary>
    /// <param name="sender">object sender (unused, required by ASP.NET) - web control that called the function</param>
    /// <param name="e">EventArgs e (unused, required by ASP.NET) - other arguments for the function</param>
    protected void RemoveCoReq_Click(object sender, EventArgs e)
    {
        //If there is no corequisite choice, return out
        if (CoreqList.SelectedItem == null)
            return;
        //Removes the chosen corequisite from the existing corequisites for the course and add it to the list of possible corequisites
        CoreqChoiceList.Items.Add(CoreqList.SelectedItem);
        CoreqList.Items.Remove(CoreqList.SelectedItem);
        CoreqChoiceList.ClearSelection();
        CoreqList.ClearSelection();
    } //end RemoveCoReq_Click()

    /// <summary>
    /// RemoveTag_Click()
    /// 
    /// Removes a tag from the list of tags for a course
    /// </summary>
    /// <param name="sender">object sender (unused, required by ASP.NET) - web control that called the function</param>
    /// <param name="e">EventArgs e (unused, required by ASP.NET) - other arguments for the function</param>
    protected void RemoveTag_Click(object sender, EventArgs e)
    {
        //If there is no tag choice, return out
        if (TagList.SelectedItem == null)
            return;
        //Removes the tag from the existing tags for the course and add it to the list of possible tags
        TagChoiceList.Items.Add(TagList.SelectedItem);
        TagList.Items.Remove(TagList.SelectedItem);
        TagChoiceList.ClearSelection();
        TagList.ClearSelection();
    } //end RemoveTagClick()

    /// <summary>
    /// CreateCourseButton_Click()
    /// 
    /// Takes the selected corequisites, prerequisites, and tags for a course and creates the course in the database
    /// </summary>
    /// <param name="sender">object sender (unused, required by ASP.NET) - web control that called the function</param>
    /// <param name="e">EventArgs e (unused, required by ASP.NET) - other arguments for the function</param>
    protected void CreateCourseButton_Click(object sender, EventArgs e)
    {
        try
        {
            Double credits = Convert.ToDouble(Credits.Text);
        }
        catch
        {
            MessageBox.Show("ERROR: Invalid credits entry!");
            //Print error message on the screen about being an invalid entry in credits
            return;
        }
        if ((SubjectList.SelectedItem == null) || CourseNumber.Text.Equals("") || CourseTitle.Text.Equals("") || Credits.Text.Equals(""))
        {
            MessageBox.Show("ERROR: Some required field is empty!");
            //Print error message about a field being empty
            return;
        }
        if (StoredProcedures.addCourse(SubjectList.SelectedItem.Value, CourseNumber.Text, CourseTitle.Text, Convert.ToDouble(Credits.Text)) == StoredProcedures.FAILURE)
        {
            MessageBox.Show("ERROR: Course could not be created for some reason...");
            //error condition
        }
        
        //Get the ID of the course that was just created and use it to add the prerequisites, corequisites, and tags to the course
        int courseID = StoredProcedures.getLastCourseID();
        addPreReqs(courseID);
        addCoReqs(courseID);
        addTags(courseID);
        MessageBox.Show("Course successfully added");
    } //end CreateCourseButton_Click()

    /////////////////////
    //Private Functions//
    /////////////////////

    /// <summary>
    /// addPreReqs()
    /// 
    /// Adds the courses in the selected Prerequisites list as prequisites for the course
    /// </summary>
    /// <param name="courseID">The ID of the course to add prequisites for</param>
    private void addPreReqs(int courseID)
    {
        foreach(ListItem i in PrereqList.Items)
        {
            if (StoredProcedures.addPrereq(i.Value, courseID.ToString()) == StoredProcedures.FAILURE)
            {
                //error condition
            }
        }
    } //end addPreReqs()

    /// <summary>
    /// addCoReqs()
    /// 
    /// Adds the courses in the selected corequisites list as corequisites for the course
    /// </summary>
    /// <param name="courseID">The ID of the course to add corequisites for</param>
    private void addCoReqs(int courseID)
    {
        foreach (ListItem i in CoreqList.Items)
        {
            if (StoredProcedures.addCoreq(i.Value, courseID.ToString()) == StoredProcedures.FAILURE)
            {
                //error condition
            }
        }
    } //end addCoReqs()

    /// <summary>
    /// addTags()
    /// 
    /// Adds the tags in the selected tag list as tags for the course
    /// </summary>
    /// <param name="courseID">The ID of the course to add tags for</param>
    private void addTags(int courseID)
    {
        foreach (ListItem i in TagList.Items)
        {
            if (StoredProcedures.addTag(courseID.ToString(), i.Value) == StoredProcedures.FAILURE)
            {
                //error condition
            }
        }
    } //end addTags
}